<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/3/12
 * Time: 09:58
 */

namespace app\api\model;


class MallSetup extends BaseModel
{
    protected $autoWriteTimestamp = true;

    public function image(){
        return $this->belongsTo('Image','image','id');
    }

    /**
     * 与thirdApp一对一关系
     * @return \think\model\relation\BelongsTo
     */
    public function thirdapp(){
        return $this->belongsTo('ThirdApp','t_id','id');
    }

    /**
     * 获取商城设置信息
     * @param $t_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getMallInfo($t_id){
        $str =  self::with(['image'])
            ->with(['thirdapp'=>['img']])
            ->where('t_id','=',$t_id)
            ->find();
        $str->visible(['name','image','thirdapp']);
        $str->thirdapp->visible(['name','sub_name','img']);
        return $str;
    }


    /**
     * 根据mini_id删除商城授权信息
     * @param $mini_id
     * @return int
     */
    public static function deleteMall($appid){
        return self::where('appid','=',$appid)->delete();
    }

    /**
     * 更新商城信息
     * @param $t_id
     * @param $data
     * @return $this
     */
    public static function updateMall($t_id,$data){
        return self::where('t_id','=',$t_id)->update($data);
    }

    /**
     * 根据管理ID获取商城设置信息
     * @param $t_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getMallSetByTid($t_id){
        return  self::where('t_id','=',$t_id)->with(['image'])->find();
    }

    /**
     * 根据appid获取商城设置信息
     * @param $appid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getMallSetByAppid($appid){
        return  self::where('appid','=',$appid)->find();
    }

}